﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using OxyPlotDemo.ViewModel;

namespace OxyPlotDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("workstation.dll", EntryPoint = "socket_init", CallingConvention = CallingConvention.Cdecl)]
        public static extern int socket_init();
        private int windowNumber = 0;
        TextBlockControl textBlockControl = new TextBlockControl()
        {
            Text = "温度(℃)",
            Status = "温度水平"
        };
        private RecommendationSystem Recommendations = new RecommendationSystem() { Recommendation = "无" };
        private Timer TimeTimer = new Timer();
        public MainWindow()
        {
            InitializeComponent();
            InitializeSensor();
            InitializeBinding();
            TimeTimer.TimerStart(1);
            if (socket_init() != 0) while (true) ;
            DataStart(1);
        }
        public void InitializeSensor()
        {
            for (int i = 0; i < 9; i++)
            {
                Common.sensorList[i].SetTypeNumber(i);
            }
        }
        public void InitializeBinding()
        {
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[0] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[0] });
            DataTextBlock1.SetBinding(TextBlock.TextProperty, new Binding("Text") { Source = textBlockControl });
            StatusTextBlock1.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = textBlockControl });
            RecommendationTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Recommendation") { Source = Recommendations });
            TimerTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("TimeNowToString") { Source = TimeTimer });
        }

        public void DataStart(double n)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(n);
            timer.Tick += Data_Tick;
            timer.Start();
        }

        private void Data_Tick(object sender, EventArgs e)
        {
            foreach (SensorData sensorData in Common.sensorList)
            {
                sensorData.GetData();
            }
            Recommendations.GiveRecommendation(Common.sensorList);
        }
        private void SimulateButton_Click(object sender, RoutedEventArgs e)
        {
            Common.sensorList[windowNumber].ResetData();
            Common.Simulation++;
            Recommendations.GiveRecommendation(Common.sensorList);
        }
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void TemperatureButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "温度(℃)";
            textBlockControl.Status = "温度水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[0] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[0] });
            windowNumber = 0;
            Common.Zoomselect = 0;
        }

        private void HumidityButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "湿度(%)";
            textBlockControl.Status = "湿度水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[1] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[1] });
            windowNumber = 1;
            Common.Zoomselect = 1;
        }

        private void PMButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "PM2.5(μg/m³)";
            textBlockControl.Status = "空气质量";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[2] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[2] });
            windowNumber = 2;
            Common.Zoomselect = 2;
        }

        private void UltravioletButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "紫外线指数";
            textBlockControl.Status = "紫外线等级";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[3] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[3] });
            windowNumber = 3;
            Common.Zoomselect = 3;
        }

        private void NoiseButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "噪音(dB)";
            textBlockControl.Status = "噪音水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[4] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[4] });
            windowNumber = 4;
            Common.Zoomselect = 4;
        }

        private void AtmosphericPressureButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "气压(hPa)";
            textBlockControl.Status = "气压水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[5] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[5] });
            windowNumber = 5;
            Common.Zoomselect = 5;
        }

        private void HeartRateButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "心率(bpm)";
            textBlockControl.Status = "心率水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[6] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[6] });
            windowNumber = 6;
            Common.Zoomselect = 6;
        }

        private void BodyTemperatureButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "体温(℃)";
            textBlockControl.Status = "体温水平";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[7] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[7] });
            windowNumber = 7;
            Common.Zoomselect = 7;
        }

        private void StepButton_Click(object sender, RoutedEventArgs e)
        {
            textBlockControl.Text = "步数";
            textBlockControl.Status = "";
            DataTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = Common.sensorList[8] });
            StatusTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = Common.sensorList[8] });
            windowNumber = 8;
            Common.Zoomselect = 8;
        }
    }
}
